package com.dd.census.controller;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.entity.SysUser;
import com.dd.census.entity.SysUserLog;
import com.dd.census.service.SysUserService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;
import com.fasterxml.jackson.databind.ObjectMapper;


@RestController
@RequestMapping(value = "census/userLogin")
public class SysUserLoginController{

	private Logger logger = LoggerFactory.getLogger(SysUserLoginController.class);

	@Autowired
	private SysUserService userService;
	
	@Autowired
	private ObjectMapper objectMapper;
	
	@PostMapping(value = "/login")
	public ResultResponse<Object> login(@RequestBody SysUser user, HttpServletRequest request) {
		try {
			SysUserLog userLog=new SysUserLog(user.getAccount(),"Login","login",objectMapper.writeValueAsString(user));
            userLog.setCreateTime(new Date());
            Map<String, Object> map = userService.selectLogin(user);
			return ResultResponse.buildSuccess(map);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping(value = "/logout", method = RequestMethod.PUT)
	public ResultResponse<Object> logout(
				@RequestHeader("token")String token, 
				@RequestBody SysUser user, 
				HttpServletRequest request) {
		try {
			// 查询用员工信息
			SysUser userNew = userService.selectById(user.getId());
			if (null == userNew) {
				throw new SysException(HttpStatus.NOT_FOUND);
			}

			// 主操作时验证认证Token和保存Token是否一致
			String tokenKey = TokenHelper.validate(user, token);
			

			// 删除Token
			if ( !"".equals(tokenKey) ) {
				TokenHelper.delete(tokenKey);
			}

			return ResultResponse.buildSuccess(request);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("updatePwd")
	public ResultResponse<Object> updatePassword(@RequestBody SysUser user,HttpServletRequest request){
		try {
			
			return ResultResponse.buildSuccess(userService.updatePassword(user));
		}catch(Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
}
